/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cirnoworks.http.utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;

/**
 *
 * @author Cloudee
 */
public class Header implements Cloneable {

    protected LinkedHashMap<String, ArrayList<String>> data = new LinkedHashMap<String, ArrayList<String>>();
    protected String protocal;

    public String getProtocal() {
        return protocal;
    }

    public void setProtocal(String protocal) {
        this.protocal = protocal;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Header other = (Header) obj;
        if (this.data != other.data && (this.data == null || !this.data.equals(other.data))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 17 * hash + (this.data != null ? this.data.hashCode() : 0);
        return hash;
    }

    public Set<Entry<String, ArrayList<String>>> entrySet() {
        return data.entrySet();
    }

    @Override
    @SuppressWarnings("unchecked")
    public Object clone() {
        try {
            Header ret = (Header) super.clone();
            ret.data = (LinkedHashMap<String, ArrayList<String>>) data.clone();
            return ret;
        } catch (CloneNotSupportedException ex) {
            throw new RuntimeException(ex);
        }
    }

    public boolean contentsKey(String key) {
        return data.containsKey(key);
    }

    public int size(String key) {
        ArrayList<String> values = data.get(key);
        if (values == null) {
            return 0;
        } else {
            return values.size();
        }
    }

    public String getLastValue(String key) {
        ArrayList<String> values = data.get(key);
        if (values == null) {
            return null;
        } else if (values.size() == 0) {
            data.remove(key);
            return null;
        }
        return values.get(values.size() - 1);
    }

    public ArrayList<String> getValues(String key) {
        return data.get(key);
    }

    public void clear(String key) {
        data.remove(key);
    }

    public void clearAll() {
        data.clear();
    }

    public void add(String key, String value) {
        ArrayList<String> values = data.get(key);
        if (values == null) {
            values = new ArrayList<String>();
        }
        values.add(value);
        data.put(key, values);
    }

    public void replaceLast(String key, String value) {
        ArrayList<String> values = data.get(key);
        if (values == null || values.size() == 0) {
            add(key, value);
        } else {
            values.set(values.size() - 1, value);
        }
    }

    public void replaceFirst(String key, String value) {
        ArrayList<String> values = data.get(key);
        if (values == null || values.size() == 0) {
            add(key, value);
        } else {
            values.set(0, value);
        }
    }

    public void put(String key, String value) {
        data.remove(key);
        add(key, value);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Entry<String, ArrayList<String>> entry : data.entrySet()) {
            String key = entry.getKey();
            ArrayList<String> values = entry.getValue();
            for (String value : values) {
                sb.append(key);
                sb.append(": ");
                sb.append(value);
                sb.append("\r\n");
            }
        }
        return sb.toString();
    }
}
